//querynew.js
//引入高德地图sdk
var amapFile = require('../../libs/amap-wx.js'); //加载高德地图sdk

//配置地图key 设置成全局配置 方便下面调用
var myAmapFun = new amapFile.AMapWX({
	key: 'a038f50d26bc347a0fa1b50834ad27dc'
});

Page({
	data: {
		markers: [
			{//起点
				iconPath: "../../img/marker.png",
				id: 0,
				latitude: 39.89491,
				longitude: 116.322056,
				width: 22,
				height: 32
			},
			{//终点
				iconPath: "../../img/marker_checked.png",
				id: 1,
				latitude: 39.875437,
				longitude: 116.477318,
				width: 22,
				height: 32
			}
		],
		longitude: '', //中心点经度
		latitude: '', //中心点纬度
		scale: 12, //缩放等级 默认是16 但是坐地铁的时候 需要加大视野
		polyline: [], //路径点
		zmq: [] //怎么去
	},
	onLoad: function(options) {
		//console.log(options.start,options.end);
		//设置地图中心点 起点作为地图中心点
		this.setFocus(options.start);
		//把起点和终点的坐标传入地图
		this.queryMap(options.start,options.end);
	},
	//设置地图中心点
	setFocus(location) {
		//把经纬度字符串拆开成经纬度数组
		let arr = location.split(",");
		this.setData({
			longitude: arr[0]
		});
		this.setData({
			latitude: arr[1]
		});
		//console.log(arr);
	},
	//显示坐标
	queryMap(start, end) {
		let startArr = start.split(",");
		let endArr = end.split(",");
		//设置起点经纬度
		this.data.markers[0].latitude = startArr[1]
		this.data.markers[0].longitude = startArr[0]
		//设置终点经纬度
		this.data.markers[1].latitude = endArr[1]
		this.data.markers[1].longitude = endArr[0]
		//渲染起点和终点
		this.setData({
			markers: this.data.markers
		});
		//画出路径 start , end 无需拆解直接传入
		this.getRoute(start,end)
	},
	//绘制路径
	getRoute(start,end) {
		let that = this;
		//通过经纬度获取路径
		myAmapFun.getDrivingRoute({
			origin: start,
			destination: end,
			success: function(data) {
				that.setData({
					zmq: data.paths[0].steps
				});
				var points = [];
				if (data.paths && data.paths[0] && data.paths[0].steps) {
					var steps = data.paths[0].steps;
					for (var i = 0; i < steps.length; i++) {
						var poLen = steps[i].polyline.split(';');
						for (var j = 0; j < poLen.length; j++) {
							points.push({
								longitude: parseFloat(poLen[j].split(',')[0]),
								latitude: parseFloat(poLen[j].split(',')[1])
							})
						}
					}
				}
				that.setData({
					polyline: [{
						points: points,
						color: "#0091ff",
						arrowLine: true, //显示箭头线，注意这个在pc模拟器中显示不出来，手机中可以
						width: 6
					}]
				});
				if (data.paths[0] && data.paths[0].distance) {
					that.setData({
						distance: data.paths[0].distance + '米'
					});
				}
			}
		})
	},
	goWeb() {
		//跳转到新日程
		console.log(123);
		//var url = '../new/new?id='+id;
		var url = '../schedule/schedule';
		wx.switchTab({
			url: url
		})
	},
	onShareAppMessage: function () {
	    return {
	        title: '欢迎使用面试精灵！',
	        desc: '导航和日程记录，让面试简单化！',
	        path: '/pages/index/index'
	    }
	}
})
